package com.xiaochong.tir.api;


import java.util.HashMap;
import java.util.Map;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.service.common.CommonBase;
import com.xiaochong.tir.api.component.actor.*;
import com.xiaochong.tir.api.util.AkkaUtil;
import com.xiaochong.tir.api.util.SpringContextUtil;
import com.xiaochong.tir.common.util.AkkaRouterUtils;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import sun.applet.Main;

/**
 * Created by cx on 2018/4/24.
 */
@SpringBootApplication
@MapperScan(ApiApp.BASE_PACKAGE)
@EnableAutoConfiguration
@ServletComponentScan
@ComponentScan({ApiApp.BASE_PACKAGE,LogTrace.BASE_PACKAGE , CommonBase.BASE_PKG})
@EnableAsync
@EnableScheduling
@EnableDiscoveryClient
@EnableFeignClients
public class ApiApp {
    public static final String BASE_PACKAGE = "com.xiaochong.tir";
    public static void main(String[] args) throws Exception {
        ApplicationContext applicationContext  = SpringApplication.run(ApiApp.class, args);
        SpringContextUtil.setApplicationContext(applicationContext);
        AkkaRouterUtils.registerAsync(AkkaUtil.AKKA_TOKEN_INDEX_RANK_GRAIL_FIVE, AkkaTokenIndexRankGrailWeightIndustryFive.class);
        AkkaRouterUtils.registerAsync(AkkaUtil.AKKA_TOKEN_INDEX_RANK_MARKET_FIVE, AkkaTokenIndexRankMoneyPopFive.class);
        AkkaRouterUtils.registerAsync(AkkaUtil.AKKA_CURRENCY_DIAGNOSIS_ASSESSMENT_FIVE, AkkaCurrencyDiagnosisAssessmentFive.class);
        AkkaRouterUtils.registerAsync(AkkaUtil.AKKA_CURRENCY_DIAGNOSIS_POPULARITY_AR_FIVE, AkkaCurrencyDiagnosisPopularityArDay.class);

        AkkaRouterUtils.registerAsync(AkkaUtil.AKKAGETMARKET_PER, AkkagetMarketPer.class);
        AkkaRouterUtils.registerAsync(AkkaUtil.PRESSURE_SUP, AkkagetPressureSup.class);
        AkkaRouterUtils.registerAsync(AkkaUtil.SPACE_TREND, AkkagetSpaceTrend.class);
        AkkaRouterUtils.registerAsync(AkkaUtil.AVG_COST, AkkagetAvgCost.class);
        AkkaRouterUtils.registerAsync(AkkaUtil.MONETARYDIAGNOSIS_TOP5, AkkMonetarydiagnosisTop5.class);
        AkkaRouterUtils.registerAsync(AkkaUtil.CONCEPT_CURRENCY, AkkageConceptCurrency.class);
        AkkaRouterUtils.registerAsync(AkkaUtil.BOTTOMTE_ZHI, AkkagetBottomteZhi.class);
    }

    /**
     *
     * attention:简单跨域就是GET，HEAD和POST请求，但是POST请求的"Content-Type"只能是application/x-www-form-urlencoded, multipart/form-data 或 text/plain
     * 反之，就是非简单跨域，此跨域有一个预检机制，说直白点，就是会发两次请求，一次OPTIONS请求，一次真正的请求
     */
    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true); // 允许cookies跨域
        config.addAllowedOrigin("*");// #允许向该服务器提交请求的URI，*表示全部允许，在SpringMVC中，如果设成*，会自动转成当前请求头中的Origin
//        config.addAllowedOrigin("123");
//        config.addAllowedOrigin("null");
        config.addAllowedHeader("*");// #允许访问的头信息,*表示全部
        config.setMaxAge(86400L);// 预检请求的缓存时间（秒），即在这个时间段里，对于相同的跨域请求不会再预检了
        config.addAllowedMethod("OPTIONS");// 允许提交请求的方法，*表示全部允许
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");// 允许Get的请求方法
//        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
//        config.addAllowedMethod("DELETE");
//        config.addAllowedMethod("PATCH");
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }

//    @Bean
//    public ServletRegistrationBean druidServlet(){
//        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
//        servletRegistrationBean.setServlet(new StatViewServlet());
//        servletRegistrationBean.addUrlMappings("/druid/*");//http://localhost:8090/datebook/druid/login.html
//        Map<String, String> initParameters = new HashMap<>();
//        initParameters.put("resetEnable", "false"); //禁用HTML页面上的“Rest All”功能
//        initParameters.put("allow", "");  //ip白名单（没有配置或者为空，则允许所有访问）
//        initParameters.put("deny", ""); //ip黑名单
//        initParameters.put("loginUsername", "druid");  //++监控页面登录用户名
//        initParameters.put("loginPassword", "tir");  //++监控页面登录用户密码
//        //如果某个ip同时存在，deny优先于allow
//        servletRegistrationBean.setInitParameters(initParameters);
//        return servletRegistrationBean;
//    }
//
//    @Bean
//    public FilterRegistrationBean filterRegistrationBean() {
//        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
//        filterRegistrationBean.setFilter(new WebStatFilter());
//        filterRegistrationBean.addUrlPatterns("/*");
//        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
//        return filterRegistrationBean;
//    }
}
